package com.dupf.upms.server.controller.upload;

import com.dupf.common.base.BaseController;
import com.dupf.upms.common.constant.UpmsResult;
import com.dupf.upms.common.constant.UpmsResultConstant;
import com.dupf.upms.rpc.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 上传controller
 * Created by dupf on 2018/3/1.
 */
@Controller
@Api(value = "上传管理", description = "上传管理")
@RequestMapping("/upload")
public class UploadController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private UpmsApiService upmsApiService;

    @ApiOperation(value = "上传凭证")
    @RequestMapping(value = "/token", method = RequestMethod.GET)
    @ResponseBody
    public Object token(@RequestParam(required = true, name = "bucket") String bucket,
                        @RequestParam(required = true, name = "module") String module,
                        HttpServletRequest request) {

        String userId = String.valueOf(request.getAttribute("currentUserId"));

        Map<String, Object> data = upmsApiService.getQiniuToken(userId, bucket, module);
        return new UpmsResult(UpmsResultConstant.SUCCESS, data);
    }

    @ApiOperation(value = "上传图片")
    @RequestMapping(value = "/log", method = RequestMethod.GET)
    @ResponseBody
    public Object log(@RequestParam(required = true, name = "module") String module,
                      @RequestParam(required = true, name = "key") String key,
                      HttpServletRequest request) {
        JSONObject result = new JSONObject();
        result.put("module", module);
        result.put("key", key);
        return new UpmsResult(UpmsResultConstant.SUCCESS, result);
    }
}
